package com.lam.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Objects;

import org.apache.commons.lang3.math.NumberUtils;

public class NumberUtil extends NumberUtils{
	
	public static final DecimalFormat DF_0 = new DecimalFormat("#");
	public static final DecimalFormat DF_2 = new DecimalFormat("#.##");
	public static final DecimalFormat DF_4 = new DecimalFormat("#.####");
	
	public static boolean eq(Integer i1, Integer i2) {
		return i1 == null || i2 == null ? false : i1.intValue() == i2.intValue();
	}
	
	public static boolean eq(Long i1, Long i2) {
		return i1 == null || i2 == null ? false : i1.longValue() == i2.longValue();
	}
	
	public static boolean eq(Double i1, Double i2) {
		return i1 == null || i2 == null ? false : i1.doubleValue() == i2.doubleValue();
	}
	
	public static boolean eq(Float i1, Float i2) {
		return i1 == null || i2 == null ? false : i1.floatValue() == i2.floatValue();
	}
	
	public static int compare(BigDecimal b1,BigDecimal b2){
		if(b1 == null){
			b1 = BigDecimal.ZERO;
		}
		if(b2 == null){
			b2 = BigDecimal.ZERO;
		}
		return b1.compareTo(b2);
	}

	public static int compare(Long l1, Long l2){
		if(l1 == null){
			l1 = 0L;
		}
		if(l2== null){
			l2 = 0L;
		}
		return l1.compareTo(l2);
	}
	
	public static int compare(Double l1, Double l2){
		if(l1 == null){
			l1 = 0D;
		}
		if(l2== null){
			l2 = 0D;
		}
		return l1.compareTo(l2);
	}

	public static Integer add(Integer i1, Integer i2){
		if(i1 == null){
			return i2;
		}
		if(i2 == null){
			return i1;
		}
		return i1 + i2;
	}
	public static Long add(Long i1, Long i2){
		if(i1 == null){
			return i2;
		}
		if(i2 == null){
			return i1;
		}
		return i1 + i2;
	}
	
	public static Boolean toBoolean(Object obj){
		return toBoolean(obj, null);
	}
	
	public static Boolean toBoolean(Object obj, Boolean defVal){
		return to(obj, defVal);
	}
	
	public static Long toLong(Object obj, Long defVal){
		return to(obj, defVal);
	}
	
	public static Long toLong(Object obj){
		return toLong(obj, null);
	}
	
	public static long toLongVal(Object obj){
		return toLong(Objects.toString(obj, null), 0L);
	}
	
	public static long toLongVal(Object obj, long defVal){
		return toLong(Objects.toString(obj, null), defVal);
	}
	
	public static int toInt(Object obj, int defVal){
		return toInt(Objects.toString(obj, null), defVal);
	}
	
	public static int toInt(Object obj){
		return toInt(Objects.toString(obj, null), 0);
	}
	
	public static Integer toInteger(Object obj){
		return toInteger(obj, null);
	}
	
	public static Integer toInteger(Object obj, Integer defVal){
		return to(obj, defVal);
	}
	
	public static Double toDouble(Object obj){
		return toDouble(obj, null);
	}
	
	public static Double toDouble(Object obj, Double defVal){
		return to(obj, defVal);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T to(Object obj, T defVal){
		if(obj == null) {
            return defVal;
        }
		String str = String.valueOf(obj);
		if((defVal instanceof Integer || defVal instanceof Long || defVal instanceof Short) && str.contains(".")){
			str = str.substring(0, str.indexOf('.'));
		}
        try {
        	if(defVal instanceof Integer){
        		return (T)Integer.valueOf(Integer.parseInt(str));
        	}
        	if(defVal instanceof Long){
        		return (T)Long.valueOf(Long.parseLong(str));
        	}
        	if(defVal instanceof Short){
        		return (T)Short.valueOf(Short.parseShort(str));
        	}
        	if(defVal instanceof Float){
        		return (T)Float.valueOf(Float.parseFloat(str));
        	}
        	if(defVal instanceof Double){
        		return (T)Double.valueOf(Double.parseDouble(str));
        	}
        	if(defVal instanceof Boolean){
        		return (T)Boolean.valueOf(Boolean.parseBoolean(str));
        	}
        	
            return defVal;
        } catch (NumberFormatException nfe) {
            return defVal;
        }
	}
	
	public static BigDecimal toBigDecimal(Object obj, String defVal){
		return createBigDecimal(Objects.toString(obj, defVal));
	}
	public static BigDecimal toBigDecimal(Object obj){
		return createBigDecimal(Objects.toString(obj, "0"));
	}
	
	public static BigDecimal add(BigDecimal b1, BigDecimal b2){
		if(b1 == null){
			return b2;
		}
		if(b2 == null){
			return b1;
		}
		return b1.add(b2);
	}
	
	public static BigDecimal subtract(BigDecimal b1, BigDecimal b2){
		if(b1 == null){
			return null;
		}
		if(b2 == null){
			return b1;
		}
		return b1.subtract(b2);
	}
	
	public static BigDecimal divide(BigDecimal b1, BigDecimal b2){
		return divide(b1, b2, 4, RoundingMode.HALF_UP);
	}
	
	public static BigDecimal divide(BigDecimal b1, BigDecimal b2, int scale){
		return divide(b1, b2, scale, RoundingMode.HALF_UP);
	}
	
	public static BigDecimal divide(BigDecimal b1, BigDecimal b2, int scale, RoundingMode roundingMode){
		if(b1 == null){
			return BigDecimal.ZERO;
		}
		if(b2 == null){
			return BigDecimal.ZERO;
		}
		return b1.divide(b2, scale, roundingMode);
	}
	
	public static BigDecimal multiply(BigDecimal b1, BigDecimal b2){
		return multiply(b1, b2, 4, RoundingMode.HALF_UP);
	}
	
	public static BigDecimal multiply(BigDecimal b1, BigDecimal b2, int scale){
		return multiply(b1, b2, scale, RoundingMode.HALF_UP);
	}
	
	public static BigDecimal multiply(BigDecimal b1, BigDecimal b2, int scale, RoundingMode roundingMode){
		if(b1 == null){
			return BigDecimal.ZERO;
		}
		if(b2 == null){
			return BigDecimal.ZERO;
		}
		return b1.multiply(b2).setScale(scale, roundingMode);
	}
	
	
	

}
